package cn.xfdzx.android.apps.shop.adapter;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListMarketBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.SearchShopBean;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

/**
 * Creator: zf.
 * Date: 2020/9/24.
 * Description:零售-搜索店铺列表适配器.
 */
public class SearchShopListRetailAdapter extends BaseQuickAdapter<SearchShopBean.DataBean, BaseViewHolder>  {
    private List<SearchShopBean.DataBean> searShopDate;
    private OnItemOnClickListener onItemOnClickListener;
    private int oldPosition = 0;
    public SearchShopListRetailAdapter(@Nullable List<SearchShopBean.DataBean> data) {
        super(R.layout.item_search_shopretail, data);
        this.searShopDate = data;
    }

    @Override
    public void setData(int index, @NonNull SearchShopBean.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final SearchShopBean.DataBean date) {
        CornerImageView iv_shop = viewHolder.getView(R.id.iv_search_shop);

        TextView tv_line = viewHolder.getView(R.id.tv_line);
        TextView tv_search_shop_name = viewHolder.getView(R.id.tv_search_shop_name);
        viewHolder.setText(R.id.tv_search_shop_name,date.getOffline_shop_name());

        if (null != date.getIn_store_photo()) {
            iv_shop.setVisibility(View.VISIBLE);
            iv_shop.setType(CornerImageView.TYPE_CIRCLE);
            ImageGlideUtil.loadImage(mContext,iv_shop,date.getIn_store_photo());
            tv_search_shop_name.setCompoundDrawables(null, null, null, null);
        }else {
            if (viewHolder.getLayoutPosition()==0){
                iv_shop.setType(CornerImageView.TYPE_ROUND);
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.ic_searchall);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tv_search_shop_name.setCompoundDrawables(drawable, null, null, null);
              //  iv_shop.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_searchall));
                iv_shop.setVisibility(View.GONE);
            }
        }

        if (date.isCheck()){
            tv_line.setVisibility(View.VISIBLE);
            tv_search_shop_name.setTextColor(mContext.getResources().getColor(R.color.color_FE4e00));
        }else {
            tv_line.setVisibility(View.INVISIBLE);
            tv_search_shop_name.setTextColor(mContext.getResources().getColor(R.color.color_393939));
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("viewHolder",viewHolder.getLayoutPosition()+"------"+searShopDate.size());
                if (oldPosition!=viewHolder.getLayoutPosition()){
                    searShopDate.get(viewHolder.getLayoutPosition()).setCheck(true);
                    if (oldPosition>=0){
                        searShopDate.get(oldPosition).setCheck(false);
                    }
                    tv_line.setVisibility(View.VISIBLE);
                    onItemOnClickListener.onItemClick(viewHolder.getLayoutPosition(),oldPosition);
                    oldPosition = viewHolder.getLayoutPosition();

                }

            }
        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<SearchShopBean.DataBean> list){
        super.replaceData(list);
        this.searShopDate = list;
    }

    public void setItemClickListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemClick(int position,int oldPosition);
    }

}
